package  cn.klaw.handler;

import com.alibaba.fastjson.JSON;
import cn.klaw.consts.ProjectEventConst;
import cn.klaw.event.ProjectEventSender;
import cn.klaw.module.ProjectEvent;
import cn.klaw.properties.EventProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.support.MessageBuilder;

/**
 * 自定义事件发布 MQ渠道
 */
@Slf4j
public class ProjectEventMQSender implements ProjectEventSender {

    private StreamBridge streamBridge;
    private EventProperty eventProperty;

    public ProjectEventMQSender(StreamBridge streamBridge, EventProperty eventProperty) {
        this.streamBridge = streamBridge;
        this.eventProperty = eventProperty;
    }

    @Override
    public void send(ProjectEvent event) {
        streamBridge.send(ProjectEventConst.DEFAULT_TOPIC_NAME + "-out-0"
                , MessageBuilder.withPayload(event)
                        // 添加自定义事件标识
                        .setHeader(ProjectEventConst.DEFAULT_CHANNEL_PREFIX, event.getChannel())
                        .build());

        if (log.isDebugEnabled()) {
            log.debug("发送MQ自定义事件: {}", JSON.toJSONString(event));
        }
    }
}
